{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dask\n",
    "## Dask.array\n",
    "A vaex dataframe can be lazily converted to a [dask.array](https://docs.dask.org/en/latest/array.html) using [DataFrame.to_dask_array](api.rst#vaex.dataframe.DataFrame.to_dask_array)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th>#                                  </th><th>x           </th><th>y           </th><th>z           </th><th>vx         </th><th>vy         </th><th>vz         </th><th>E              </th><th>L                 </th><th>Lz                 </th><th>FeH                </th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td><i style='opacity: 0.6'>0</i>      </td><td>-0.777470767</td><td>2.10626292  </td><td>1.93743467  </td><td>53.276722  </td><td>288.386047 </td><td>-95.2649078</td><td>-121238.171875 </td><td>831.0799560546875 </td><td>-336.426513671875  </td><td>-2.309227609164518 </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>1</i>      </td><td>3.77427316  </td><td>2.23387194  </td><td>3.76209331  </td><td>252.810791 </td><td>-69.9498444</td><td>-56.3121033</td><td>-100819.9140625</td><td>1435.1839599609375</td><td>-828.7567749023438 </td><td>-1.788735491591229 </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>2</i>      </td><td>1.3757627   </td><td>-6.3283844  </td><td>2.63250017  </td><td>96.276474  </td><td>226.440201 </td><td>-34.7527161</td><td>-100559.9609375</td><td>1039.2989501953125</td><td>920.802490234375   </td><td>-0.7618109022478798</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>3</i>      </td><td>-7.06737804 </td><td>1.31737781  </td><td>-6.10543537 </td><td>204.968842 </td><td>-205.679016</td><td>-58.9777031</td><td>-70174.8515625 </td><td>2441.724853515625 </td><td>1183.5899658203125 </td><td>-1.5208778422936413</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>4</i>      </td><td>0.243441463 </td><td>-0.822781682</td><td>-0.206593871</td><td>-311.742371</td><td>-238.41217 </td><td>186.824127 </td><td>-144138.75     </td><td>374.8164367675781 </td><td>-314.5353088378906 </td><td>-2.655341358427361 </td></tr>\n",
       "<tr><td>...                                </td><td>...         </td><td>...         </td><td>...         </td><td>...        </td><td>...        </td><td>...        </td><td>...            </td><td>...               </td><td>...                </td><td>...                </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>329,995</i></td><td>3.76883793  </td><td>4.66251659  </td><td>-4.42904139 </td><td>107.432999 </td><td>-2.13771296</td><td>17.5130272 </td><td>-119687.3203125</td><td>746.8833618164062 </td><td>-508.96484375      </td><td>-1.6499842518381402</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>329,996</i></td><td>9.17409325  </td><td>-8.87091351 </td><td>-8.61707687 </td><td>32.0       </td><td>108.089264 </td><td>179.060638 </td><td>-68933.8046875 </td><td>2395.633056640625 </td><td>1275.490234375     </td><td>-1.4336036247720836</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>329,997</i></td><td>-1.14041007 </td><td>-8.4957695  </td><td>2.25749826  </td><td>8.46711349 </td><td>-38.2765236</td><td>-127.541473</td><td>-112580.359375 </td><td>1182.436279296875 </td><td>115.58557891845703 </td><td>-1.9306227597361942</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>329,998</i></td><td>-14.2985935 </td><td>-5.51750422 </td><td>-8.65472317 </td><td>110.221558 </td><td>-31.3925591</td><td>86.2726822 </td><td>-74862.90625   </td><td>1324.5926513671875</td><td>1057.017333984375  </td><td>-1.225019818838568 </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>329,999</i></td><td>10.5450506  </td><td>-8.86106777 </td><td>-4.65835428 </td><td>-2.10541415</td><td>-27.6108856</td><td>3.80799961 </td><td>-95361.765625  </td><td>351.0955505371094 </td><td>-309.81439208984375</td><td>-2.5689636894079477</td></tr>\n",
       "</tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "#        x             y             z             vx           vy           vz           E                L                   Lz                   FeH\n",
       "0        -0.777470767  2.10626292    1.93743467    53.276722    288.386047   -95.2649078  -121238.171875   831.0799560546875   -336.426513671875    -2.309227609164518\n",
       "1        3.77427316    2.23387194    3.76209331    252.810791   -69.9498444  -56.3121033  -100819.9140625  1435.1839599609375  -828.7567749023438   -1.788735491591229\n",
       "2        1.3757627     -6.3283844    2.63250017    96.276474    226.440201   -34.7527161  -100559.9609375  1039.2989501953125  920.802490234375     -0.7618109022478798\n",
       "3        -7.06737804   1.31737781    -6.10543537   204.968842   -205.679016  -58.9777031  -70174.8515625   2441.724853515625   1183.5899658203125   -1.5208778422936413\n",
       "4        0.243441463   -0.822781682  -0.206593871  -311.742371  -238.41217   186.824127   -144138.75       374.8164367675781   -314.5353088378906   -2.655341358427361\n",
       "...      ...           ...           ...           ...          ...          ...          ...              ...                 ...                  ...\n",
       "329,995  3.76883793    4.66251659    -4.42904139   107.432999   -2.13771296  17.5130272   -119687.3203125  746.8833618164062   -508.96484375        -1.6499842518381402\n",
       "329,996  9.17409325    -8.87091351   -8.61707687   32.0         108.089264   179.060638   -68933.8046875   2395.633056640625   1275.490234375       -1.4336036247720836\n",
       "329,997  -1.14041007   -8.4957695    2.25749826    8.46711349   -38.2765236  -127.541473  -112580.359375   1182.436279296875   115.58557891845703   -1.9306227597361942\n",
       "329,998  -14.2985935   -5.51750422   -8.65472317   110.221558   -31.3925591  86.2726822   -74862.90625     1324.5926513671875  1057.017333984375    -1.225019818838568\n",
       "329,999  10.5450506    -8.86106777   -4.65835428   -2.10541415  -27.6108856  3.80799961   -95361.765625    351.0955505371094   -309.81439208984375  -2.5689636894079477"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import vaex\n",
    "df = vaex.example()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<tr>\n",
       "<td>\n",
       "<table>\n",
       "  <thead>\n",
       "    <tr><td> </td><th> Array </th><th> Chunk </th></tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr><th> Bytes </th><td> 7.92 MB </td> <td> 7.92 MB </td></tr>\n",
       "    <tr><th> Shape </th><td> (330000, 3) </td> <td> (330000, 3) </td></tr>\n",
       "    <tr><th> Count </th><td> 2 Tasks </td><td> 1 Chunks </td></tr>\n",
       "    <tr><th> Type </th><td> float64 </td><td> numpy.ndarray </td></tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</td>\n",
       "<td>\n",
       "<svg width=\"75\" height=\"170\" style=\"stroke:rgb(0,0,0);stroke-width:1\" >\n",
       "\n",
       "  <!-- Horizontal lines -->\n",
       "  <line x1=\"0\" y1=\"0\" x2=\"25\" y2=\"0\" style=\"stroke-width:2\" />\n",
       "  <line x1=\"0\" y1=\"120\" x2=\"25\" y2=\"120\" style=\"stroke-width:2\" />\n",
       "\n",
       "  <!-- Vertical lines -->\n",
       "  <line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"120\" style=\"stroke-width:2\" />\n",
       "  <line x1=\"25\" y1=\"0\" x2=\"25\" y2=\"120\" style=\"stroke-width:2\" />\n",
       "\n",
       "  <!-- Colored Rectangle -->\n",
       "  <polygon points=\"0.000000,0.000000 25.412617,0.000000 25.412617,120.000000 0.000000,120.000000\" style=\"fill:#ECB172A0;stroke-width:0\"/>\n",
       "\n",
       "  <!-- Text -->\n",
       "  <text x=\"12.706308\" y=\"140.000000\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" >3</text>\n",
       "  <text x=\"45.412617\" y=\"60.000000\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" transform=\"rotate(-90,45.412617,60.000000)\">330000</text>\n",
       "</svg>\n",
       "</td>\n",
       "</tr>\n",
       "</table>"
      ],
      "text/plain": [
       "dask.array<vaex-df-d741baee-10eb-11ea-b19a, shape=(330000, 3), dtype=float64, chunksize=(330000, 3), chunktype=numpy.ndarray>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# convert a set of columns in the dataframe to a 2d dask array\n",
    "A = df[['x', 'y', 'z']].to_dask_array()\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<tr>\n",
       "<td>\n",
       "<table>\n",
       "  <thead>\n",
       "    <tr><td> </td><th> Array </th><th> Chunk </th></tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr><th> Bytes </th><td> 2.64 MB </td> <td> 2.64 MB </td></tr>\n",
       "    <tr><th> Shape </th><td> (330000,) </td> <td> (330000,) </td></tr>\n",
       "    <tr><th> Count </th><td> 11 Tasks </td><td> 1 Chunks </td></tr>\n",
       "    <tr><th> Type </th><td> float64 </td><td> numpy.ndarray </td></tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</td>\n",
       "<td>\n",
       "<svg width=\"170\" height=\"75\" style=\"stroke:rgb(0,0,0);stroke-width:1\" >\n",
       "\n",
       "  <!-- Horizontal lines -->\n",
       "  <line x1=\"0\" y1=\"0\" x2=\"120\" y2=\"0\" style=\"stroke-width:2\" />\n",
       "  <line x1=\"0\" y1=\"25\" x2=\"120\" y2=\"25\" style=\"stroke-width:2\" />\n",
       "\n",
       "  <!-- Vertical lines -->\n",
       "  <line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"25\" style=\"stroke-width:2\" />\n",
       "  <line x1=\"120\" y1=\"0\" x2=\"120\" y2=\"25\" style=\"stroke-width:2\" />\n",
       "\n",
       "  <!-- Colored Rectangle -->\n",
       "  <polygon points=\"0.000000,0.000000 120.000000,0.000000 120.000000,25.412617 0.000000,25.412617\" style=\"fill:#ECB172A0;stroke-width:0\"/>\n",
       "\n",
       "  <!-- Text -->\n",
       "  <text x=\"60.000000\" y=\"45.412617\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" >330000</text>\n",
       "  <text x=\"140.000000\" y=\"12.706308\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" transform=\"rotate(0,140.000000,12.706308)\">1</text>\n",
       "</svg>\n",
       "</td>\n",
       "</tr>\n",
       "</table>"
      ],
      "text/plain": [
       "dask.array<sqrt, shape=(330000,), dtype=float64, chunksize=(330000,), chunktype=numpy.ndarray>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import dask.array as da\n",
    "# lazily compute with dask\n",
    "r = da.sqrt(A[:,0]**2 + A[:,1]**2 + A[:,2]**2)\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# materialize the data\n",
    "r_computed = r.compute()\n",
    "r_computed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th>#                                  </th><th>x           </th><th>y           </th><th>z           </th><th>vx         </th><th>vy         </th><th>vz         </th><th>E              </th><th>L                 </th><th>Lz                 </th><th>FeH                </th><th>r                 </th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td><i style='opacity: 0.6'>0</i>      </td><td>-0.777470767</td><td>2.10626292  </td><td>1.93743467  </td><td>53.276722  </td><td>288.386047 </td><td>-95.2649078</td><td>-121238.171875 </td><td>831.0799560546875 </td><td>-336.426513671875  </td><td>-2.309227609164518 </td><td>2.9655450396553587</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>1</i>      </td><td>3.77427316  </td><td>2.23387194  </td><td>3.76209331  </td><td>252.810791 </td><td>-69.9498444</td><td>-56.3121033</td><td>-100819.9140625</td><td>1435.1839599609375</td><td>-828.7567749023438 </td><td>-1.788735491591229 </td><td>5.77829281049018  </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>2</i>      </td><td>1.3757627   </td><td>-6.3283844  </td><td>2.63250017  </td><td>96.276474  </td><td>226.440201 </td><td>-34.7527161</td><td>-100559.9609375</td><td>1039.2989501953125</td><td>920.802490234375   </td><td>-0.7618109022478798</td><td>6.99079603950256  </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>3</i>      </td><td>-7.06737804 </td><td>1.31737781  </td><td>-6.10543537 </td><td>204.968842 </td><td>-205.679016</td><td>-58.9777031</td><td>-70174.8515625 </td><td>2441.724853515625 </td><td>1183.5899658203125 </td><td>-1.5208778422936413</td><td>9.431842752707537 </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>4</i>      </td><td>0.243441463 </td><td>-0.822781682</td><td>-0.206593871</td><td>-311.742371</td><td>-238.41217 </td><td>186.824127 </td><td>-144138.75     </td><td>374.8164367675781 </td><td>-314.5353088378906 </td><td>-2.655341358427361 </td><td>0.8825613121347967</td></tr>\n",
       "<tr><td>...                                </td><td>...         </td><td>...         </td><td>...         </td><td>...        </td><td>...        </td><td>...        </td><td>...            </td><td>...               </td><td>...                </td><td>...                </td><td>...               </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>329,995</i></td><td>3.76883793  </td><td>4.66251659  </td><td>-4.42904139 </td><td>107.432999 </td><td>-2.13771296</td><td>17.5130272 </td><td>-119687.3203125</td><td>746.8833618164062 </td><td>-508.96484375      </td><td>-1.6499842518381402</td><td>7.453831761514681 </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>329,996</i></td><td>9.17409325  </td><td>-8.87091351 </td><td>-8.61707687 </td><td>32.0       </td><td>108.089264 </td><td>179.060638 </td><td>-68933.8046875 </td><td>2395.633056640625 </td><td>1275.490234375     </td><td>-1.4336036247720836</td><td>15.398412491068198</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>329,997</i></td><td>-1.14041007 </td><td>-8.4957695  </td><td>2.25749826  </td><td>8.46711349 </td><td>-38.2765236</td><td>-127.541473</td><td>-112580.359375 </td><td>1182.436279296875 </td><td>115.58557891845703 </td><td>-1.9306227597361942</td><td>8.864250273925633 </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>329,998</i></td><td>-14.2985935 </td><td>-5.51750422 </td><td>-8.65472317 </td><td>110.221558 </td><td>-31.3925591</td><td>86.2726822 </td><td>-74862.90625   </td><td>1324.5926513671875</td><td>1057.017333984375  </td><td>-1.225019818838568 </td><td>17.601047186042507</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>329,999</i></td><td>10.5450506  </td><td>-8.86106777 </td><td>-4.65835428 </td><td>-2.10541415</td><td>-27.6108856</td><td>3.80799961 </td><td>-95361.765625  </td><td>351.0955505371094 </td><td>-309.81439208984375</td><td>-2.5689636894079477</td><td>14.540181524970293</td></tr>\n",
       "</tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "#        x             y             z             vx           vy           vz           E                L                   Lz                   FeH                  r\n",
       "0        -0.777470767  2.10626292    1.93743467    53.276722    288.386047   -95.2649078  -121238.171875   831.0799560546875   -336.426513671875    -2.309227609164518   2.9655450396553587\n",
       "1        3.77427316    2.23387194    3.76209331    252.810791   -69.9498444  -56.3121033  -100819.9140625  1435.1839599609375  -828.7567749023438   -1.788735491591229   5.77829281049018\n",
       "2        1.3757627     -6.3283844    2.63250017    96.276474    226.440201   -34.7527161  -100559.9609375  1039.2989501953125  920.802490234375     -0.7618109022478798  6.99079603950256\n",
       "3        -7.06737804   1.31737781    -6.10543537   204.968842   -205.679016  -58.9777031  -70174.8515625   2441.724853515625   1183.5899658203125   -1.5208778422936413  9.431842752707537\n",
       "4        0.243441463   -0.822781682  -0.206593871  -311.742371  -238.41217   186.824127   -144138.75       374.8164367675781   -314.5353088378906   -2.655341358427361   0.8825613121347967\n",
       "...      ...           ...           ...           ...          ...          ...          ...              ...                 ...                  ...                  ...\n",
       "329,995  3.76883793    4.66251659    -4.42904139   107.432999   -2.13771296  17.5130272   -119687.3203125  746.8833618164062   -508.96484375        -1.6499842518381402  7.453831761514681\n",
       "329,996  9.17409325    -8.87091351   -8.61707687   32.0         108.089264   179.060638   -68933.8046875   2395.633056640625   1275.490234375       -1.4336036247720836  15.398412491068198\n",
       "329,997  -1.14041007   -8.4957695    2.25749826    8.46711349   -38.2765236  -127.541473  -112580.359375   1182.436279296875   115.58557891845703   -1.9306227597361942  8.864250273925633\n",
       "329,998  -14.2985935   -5.51750422   -8.65472317   110.221558   -31.3925591  86.2726822   -74862.90625     1324.5926513671875  1057.017333984375    -1.225019818838568   17.601047186042507\n",
       "329,999  10.5450506    -8.86106777   -4.65835428   -2.10541415  -27.6108856  3.80799961   -95361.765625    351.0955505371094   -309.81439208984375  -2.5689636894079477  14.540181524970293"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# put it back in the dataframe\n",
    "df['r'] = r_computed\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
